package com.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.dao.UsersDao;
import com.model.Message;
import com.model.Users;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.service.MessageService;
import com.service.UsersService;



public class UsersAction extends ActionSupport implements ModelDriven<Users>, ServletRequestAware {

	private Users users;
	private UsersService usersService;
	private HttpServletRequest request;
	private MessageService messageService;
	
	public MessageService getMessageService() {
		return messageService;
	}

	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}

	public Users getUsers() {
		return users;
	}

	public void setUsers(Users users) {
		this.users = users;
	}

	public UsersService getUsersService() {
		return usersService;
	}

	public void setUsersService(UsersService usersService) {
		this.usersService = usersService;
	}

	@Override
	public Users getModel() {
		users = new Users();
		return users;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.request=arg0;		
	}
	
	public String add() {
		usersService.save(users);
		return SUCCESS;
		
	}
	public String login() throws Exception {
		
		if (usersService.found(users.getUserName(), users.getPassword())!=null) {
			request.getSession().setAttribute("user", users);
			return "success";
		} else {
			return "error";
		}
	}
	
	public String show() throws Exception {
		ArrayList<Users> list = usersService.getAll();
		request.setAttribute("userslist",list);
		return "success";
	}
	
	public String showm() throws Exception {
		ArrayList<Message> list =messageService.getAll();
		request.setAttribute("messagelist",list);
		return "success";
	}
	
}
